<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BAQQueueLine
 *
 * @author I062261
 */
class BAQQueueLine {
    //put your code here

    const key_QueueLineId = "queueLineId";
    const key_ServiceId = "serviceId";
    const key_Resources = 'resources';
    const makeQueueStatus_MakeQueue = 'makeQueue';
    const makeQueueStatus_MakeQueueTime = 'makeQueueTime';
    const makeQueueStatus_cancelQueueTime = 'cancelQueueTime';
    const makeQueueStatus_Canceled = 'canceled';
    const makeQueueStatus_CancelTime = 'cancelTime';
    const processQueueStatus_Pending = 'pending';
    const processQueueStatus_Processing = 'processing';
    const processQueueStatus_StartProcessTime = 'startProcessTime';
    const processQueueStatus_Processed = 'processed';
    const processQueueStatus_EndProcessTime = 'endProcessTime';
    const QueueLineStatus_Expired = 'expired';
    const QueueLineStatus_Active = 'active';

    public $id;
    public $name;
    public $description;
    public $serviceId;
    protected $childQLines = array();
    protected $resources = array();
    protected $timeResource;
    public $currentTransaction;
    public $data = array();

    public static function findAll($data = array()) {
        $mQLines = array();
        $serviceId = isset($data[self::key_ServiceId]) ? $data[self::key_ServiceId] : NULL;
        $status = isset($data['status']) ? $data['status'] : self::QueueLineStatus_Active;

        if ($serviceId != NULL)
            $mQLines = BMAppOnlineQueueServiceQueueLine::model()->findAllByAttributes(array('service_id' => $serviceId, 'status' => $status));

        $queueLines = array();

        foreach ($mQLines as $mQLine) {
            $queueLine = BAQQueueLine::find(array(BAQQueueLine::key_QueueLineId => $mQLine->queueline_id));
            //       $queueLine->id = $mQLine->queueline_id;
            //       $queueLine->description = $mQLine->description;
            $queueLine->serviceId = $mQLine->service_id;
            $queueLine->data = $mQLine->data;

            array_push($queueLines, $queueLine);
        }
        return $queueLines;
    }

    /**
     * 
     * @param type $data
     * @return \BAQQueueLine
     */
    public static function find($data = array()) {
        $queueLine = new BAQQueueLine();

        $qlId = isset($data[self::key_QueueLineId]) ? $data[self::key_QueueLineId] : NULL;

        if ($qlId != NULL) {
            $mQLine = BMAppOnlineQueueQueueLine::model()->findByAttributes(array('_id' => $qlId));
            $queueLine->id = $mQLine->_id;
            $queueLine->name = $mQLine->name;
            $queueLine->description = $mQLine->description;
            //        $queueLine->serviceId = $mQLine->service_id;
            $queueLine->data = $mQLine->data;
        }

        return $queueLine;
    }

    public function getChildrenQueueLines() {

        $childLines = array();
        $mChildQLines = BMServiceQueueLineChild::model()->findAllByAttributes(array('queueline_id' => $this->id));

        foreach ($mChildQLines as $mQLine) {
            $qLine = BAQQueueLine::find(array(self::key_QueueLineId => $mQLine->child_id));
            array_push($childLines, $qLine);
        }

        return $childLines;
    }

    public function getServiceQueueLineId() {
        $serviceId = $this->serviceId == NULL ? Blyn::app()->getActiveService()->getId() : $this->serviceId;
        $mServiceQueueLine = BMAppOnlineQueueServiceQueueLine::model()->findByAttributes(array('queueline_id' => $this->id, 'service_id' => $serviceId));
        return $mServiceQueueLine == NULL ? FALSE : $mServiceQueueLine->_id;
    }

    public function getResources($data = array()) {

        $resources = array();

        $sQL_id = $this->getServiceQueueLineId();

        $mServiceQLineResources = BMServiceQueueLineResource::model()->findAllByAttributes(array('service_queueline_id' => $sQL_id));

        foreach ($mServiceQLineResources as $mResource) {
            $resource = BAQResource::find(array(BAQResource::Key_Resource_Id => $mResource->_id));

            $resources[$resource->id] = $resource;
        }

        return $resources;
    }

    public function getTransactions($data = array()) {

        $makeQueueTime = isset($data['makeQueueTime']) ? $data['makeQueueTime'] : NULL;
        $processQueueTime = isset($data['processQueueTime']) ? $data['processQueueTime'] : NULL;

        $queueLineId = isset($data[self::key_QueueLineId]) ? $data[self::key_QueueLineId] : $this->id;
        $resources = isset($data[self::key_Resources]) ? $data[self::key_Resources] : $this->getResources();
        $serviceId = Blyn::app()->getActiveService()->getId();
        $serviceQueueLine = BMAppOnlineQueueServiceQueueLine::model()->findByAttributes(array('service_id' => $serviceId, 'queueline_id' => $queueLineId));
        $serviceQueueLineId = $serviceQueueLine == NULL ? FALSE : $serviceQueueLine->_id;

        $transactions = array();

        $db = Blyn::app()->getActiveServiceApp()->getDbAdapter();

        if ($serviceQueueLineId != FALSE) {
            $mixTransactions = $db->findTransaction(array('id' => $serviceQueueLineId), $resources, array('makeQueueTime' => $makeQueueTime, 'processQueueTime' => $processQueueTime));

            $currentTransactions = $mixTransactions['currentTransactions'];
            $currentCycles = $mixTransactions['currentCycles'];

            foreach ($currentTransactions as $mTransaction) {
                $tran = new BAQTransaction();

                $tran->id = $mTransaction->_id;
                $tran->sequence = $mTransaction->transaction_sequence;
                $tran->resourceId = $mTransaction->service_queueline_resource_id;
                $tran->startTime = $mTransaction->start_time;
                $tran->endTime = $mTransaction->end_time;
                $tran->status = $mTransaction->transaction_status;
                $tran->data = $mTransaction->data;

                $tran->currentCycle = current($currentCycles);
                next($currentCycles);

                $transactions[$tran->id] = $tran;
            }
        }

        return $transactions;
    }

    public static function date_system_timezone_set() {
        $shell = new COM("WScript.Shell") or die("Requires Windows Scripting Host");
        $time_bias = -($shell->RegRead(
                        "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation\\Bias")) / 60;
        $ab = -($shell->RegRead(
                        "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation\\ActiveTimeBias")) / 60;
        $sc = $shell->RegRead(
                "HKEY_USERS\\.DEFAULT\\Control Panel\\International\\sCountry");
        foreach (timezone_abbreviations_list() as $tza)
            foreach ($tza as $entry) {
                $country = strtok($entry['timezone_id'], '/');
                $locale = strtok('|');
                if ($country == $sc && $ab == ($entry['offset'] / 60 / 60) && ($ds = $time_bias != $ab) == $entry['dst']) {
                    date_default_timezone_set($timezone_identifier = $country . "/" . $locale);
                    return sprintf('%.1f', $ab) . "/" . ($ds ? '' : 'no ') . 'DST' . " " . $timezone_identifier;
                }
            }
        return false;
    }
    
    

}

?>
